package com.example.demolistview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends Activity {

	ListView list1;
	String[] listMessages;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_view);
		
		try {
			
			//get data and view 
			list1 = (ListView) findViewById(R.id.lv1);
			listMessages = getResources().getStringArray(R.array.list_messages);
			
			//initialize to adapder
			MessageAdapter myAdapter = new MessageAdapter(this, listMessages);
			
			//assign adapter to the listview
			list1.setAdapter(myAdapter);
			
			//set up event when click to listview's item
			list1.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position,
						long id) {
					Log.e("ChooseItem","");
					Toast.makeText(ListViewActivity.this, listMessages[position],
							Toast.LENGTH_SHORT).show();
					Intent intent  = new Intent(ListViewActivity.this,DetailMessageActivity.class);
					intent.putExtra("position", position);
					startActivity(intent);
				}
			});
		} catch (Exception e) {
			Log.e("Loi",e.toString());
		}
		
		
		
	}
}
